<?php

namespace App\Http\Controllers\Admin\Shop;

use App\Http\Requests\Shop\ProductStore;
use App\Models\Shop\Product;
use App\Models\Shop\ProductGallery;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{
   /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
   public function index(Request $request)
   {
      $where = function ($query) use ($request) {
         if ($request->has('keyword') and $request->keyword != '') {
            $cate = $request->cate;
            $search = "%" . $request->keyword . "%";
            $query->where('a.' . $cate, 'like', $search);
         }
         //按分类搜索
         if ($request->has('category_id') and $request->category_id != '') {
            $query->where('a.' . 'category_id', $request->category_id);
         }
         //按状态搜索
         if ($request->has('check') && $request->check != '') {
            $checks = explode(',', $request->check);
            foreach ($checks as $item) {
               $query->where('a.' . $item, 1);
            }
         }
         //上架状态搜索
         if ($request->has('is_onsale') && $request->is_onsale != '') {
            $query->where('a.' . 'is_onsale', $request->is_onsale);
         }
         //按时间搜索
         if ($request->has('date') && $request->date != '' && $request->date != null) {
            $query->whereBetween('a.' . 'created_at', explode(',', $request->date));
         }
      };

      $products = DB::table('products as a')
         ->join('categories as b', 'a.category_id', '=', 'b.id')
         ->select('a.*', 'b.name as category_name')
         ->where($where)
         ->orderBy('a.sort_order', 'desc')
         ->orderBy('a.created_at', 'desc')
         ->paginate(config('admin.page_size'));

      return response()->json($products);
   }

   /**
    * Store a newly created resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @return \Illuminate\Http\Response
    */
   public function store(ProductStore $request)
   {
      $product = Product::create($request->all());

      //相册存到商品相册表
      if ($request->has('product_id')) {
         foreach ($request->product_id as $item) {
            $product->galleries()->create(['image' => $item['image'], 'sort' => $item['sort']]);
         }
      }

      if ($product) {
         $info = ['status' => 1, 'msg' => '新增商品成功~'];
         return response()->json($info);
      }
   }

   /**
    * Display the specified resource.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function show($id)
   {
      $product = Product::with(['galleries' => function ($query) {
         $query->orderBy('sort');
      }])->find($id);

      return response()->json($product);
   }

   /**
    * Update the specified resource in storage.
    *
    * @param \Illuminate\Http\Request $request
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function update(Request $request, $id)
   {
      $product = Product::find($id);
      $result = $product->update($request->all());

      //将客照插入glleries表
      if ($request->has('product_id')) {
         ProductGallery::where('product_id', '=', $id)->delete();
         foreach ($request->product_id as $item) {
            $product->galleries()->create(['image' => $item['image'], 'sort' => $item['sort']]);
         }
      }

      if ($result) {
         $info = ['status' => 1, 'msg' => '更新商品成功~'];
         return response()->json($info);
      }
   }

   /**
    * Remove the specified resource from storage.
    *
    * @param int $id
    * @return \Illuminate\Http\Response
    */
   public function destroy($id)
   {
      Product::destroy($id);
      DB::table('product_galleries')->where('product_id', $id)->delete();  //删除商品相册表
   }

   /***
    *  改变属性
    */
   public function change_attr(Request $request)
   {
      $product = Product::find($request->id);
      $attr = $request->attr;
      $product->$attr = $product->$attr ? false : true;
      $product->save();
   }

   /***
    *  排序，改变库存
    */
   public function change(Request $request)
   {
      $product = Product::find($request->id);
      $attr = $request->attr;
      $product->$attr = $request->row;
      $product->save();
   }

   /***
    *  多选删除
    */
   public function delete_all(Request $request)
   {
      Product::destroy($request->checked_id);
   }

   /****
    *  删除客照表客照
    */
   public function delete_img(Request $request)
   {
      $gallery = ProductGallery::destroy($request->id);
      if ($gallery) {
         $info = ['status' => 1, 'msg' => '删除客照成功~'];
         return response()->json($info);
      }
   }
}
